home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 1 / ETO Development Tools 1.iso / Essentials / Developer Essentials Jul 90 / Technical Docs / Apple II Technical Notes / Technical Notes (Text) / IIGS / TN.IIGS.038 < prev    next >
Encoding:
Text File  |  1990-04-03  |  9.0 KB  |  280 lines  |  [TEXT/pdos]

  1. Apple II
  2. Technical Notes
  3. _____________________________________________________________________________
  4.                                                   Developer Technical Support
  5.  
  6. Apple IIGS
  7. #38:    List Controls in Dialog Boxes
  8.  
  9. Revised by:    Dave Lyons & Eric Soldan                            March 1990
  10. Written by:    Keith Rollin                                          May 1988
  11.  
  12. This Technical Note describes how to include a list control into a dialog box.
  13. Sample APW C source code is included.
  14. Changes since July 1989:  Added note that only one list control per modal 
  15. dialog is supported, and that the list must be added after all the dialog 
  16. items are added.
  17. _____________________________________________________________________________
  18.  
  19. The need to put a list control into a dialog box is obvious.  The Print 
  20. Manager does it.  The Font Manager does it.  You may want to use one in your 
  21. own application to manage a list of data base fields or spreadsheet functions.  
  22. However, performing the task is not as obvious as the need.
  23.  
  24. Given the features of TaskMaster in System Software 5.0, it is now much easier 
  25. to emulate a modal dialog in a normal window.  If you need to add a list 
  26. control to a modal dialog, you should seriously consider emulating a modal 
  27. dialog with a normal window instead of using the Dialog Manager.  If you use 
  28. the Dialog Manager, the following procedure and sample C fragment illustrate 
  29. the technique necessary for adding a list control.
  30.  
  31. Note that only one list control is allowed in a modal dialog.  If you need 
  32. more than one, the Dialog Manager cannot help you--create a normal window 
  33. instead.
  34.  
  35.  
  36. Individual Steps
  37.  
  38. Basically, there are three check-off items for putting a list control into a 
  39. dialog box:
  40.  
  41. 1.  You must install the list explicitly into the dialog box yourself.  
  42.     This should be done after you have created the dialog box with a 
  43.     call to NewModalDialog or GetNewModalDialog.  Do not install it as 
  44.     a UserItem or UserCtlItem.  Installing it as a UserItem would 
  45.     cause the Dialog Manager to place an invisible custom control over 
  46.     the list, preventing later use of FindControl to manage it.  
  47.     Installing the list as a UserCtlItem does not allow the list 
  48.     control to be properly initialized.
  49.  
  50.     Note:  After you add the list control, you must not add any more 
  51.            dialog items.
  52.  
  53.     InitValues()
  54.     {
  55.         /* Get a Full Screen, invisible dialog window with only
  56.            a Quit button in it*/
  57.         myDialog = GetNewModalDialog(&PrintDialog);
  58.     
  59.  
  60.         /* Add this List Control ourselves */
  61.         myListHndl = CreateList(myDialog,&myList);
  62.     
  63.         /* Get the handle for the Scrollbar Control */
  64.         listScrollHandle = (**myListHndl).ctlListBar;
  65.     
  66.  
  67.         /* Save and Zero out the RefCons */
  68.         listRefCons = GetCtlRefCon(myListHndl);
  69.         scrollRefCons = GetCtlRefCon(listScrollHandle);
  70.         ZeroRefCons(); /* This is explained below in item #3 */
  71.     
  72.         /* Now show the dialog box */
  73.         ShowWindow(myDialog);
  74.     }
  75.  
  76. 2.  Because the list control is not a dialog item, a custom FilterProc 
  77.     must be installed for ModalDialog to test for mouse-down events.  
  78.     Pass the address of this routine (with the high bit set so that 
  79.     default handling of items is in effect) when you call ModalDialog.
  80.  
  81.     pascal Word myFilterProc(theDialog, theEvent, theItem)
  82.         GrafPortPtr    theDialog;
  83.         EventRecord    *theEvent;
  84.         long            *theItem;
  85.     
  86.     {
  87.         CtlRecHndl  tHandle;
  88.       
  89.         if ((*theEvent).what == mouseDownEvt) {
  90.             FindControl(&tHandle,(*theEvent).where,theDialog);
  91.             if ((tHandle == myListHndl) || (tHandle == listScrollHandle)) {
  92.     
  93.             /* Set the RefCons back to the way the list manager likes them */
  94.                 RestoreRefCons();
  95.                 TrackControl((*theEvent).where,(LongProcPtr) -1, tHandle);
  96.                 ZeroRefCons();
  97.     
  98.                 /* Tell the Dialog Manager that we handled this event */
  99.                 return(true);
  100.             }
  101.         }
  102.         /* We didn't do anything, so return false to get Dialog Manager
  103.             to handle this event */
  104.       return(false);
  105.     }
  106.  
  107. 3.  The Dialog Manager uses the RefCon field of its items (all of 
  108.     which are installed as controls).  Unfortunately, the List Manager 
  109.     also uses the RefCon field for its own purposes.  This shared use 
  110.     means that a judicious juggling of those values is required.  This 
  111.     juggling is the reason for the two routines RestoreRefCons and 
  112.     ZeroRefCons used above.
  113.  
  114.     /* Zero out the RefCons for the Dialog Manager */
  115.     ZeroRefCons()
  116.     {
  117.         SetCtlRefCon(0,myListHndl);
  118.         SetCtlRefCon(0,listScrollHandle);
  119.     }
  120.     
  121.  
  122.     /* Restore the RefCons for the List Manager */
  123.     RestoreRefCons()
  124.     {
  125.         SetCtlRefCon(listRefCons,myListHndl);
  126.         SetCtlRefCon(scrollRefCons,listScrollHandle);
  127.     }
  128.  
  129. Note:  Because the Dialog Manager currently uses the RefCon to keep track 
  130.        of which dialog item is identified with which particular control, 
  131.        zeroing the RefCon fields can cause a little confusion.  
  132.        Specifically, those who would like to do GetFirstDItem from within 
  133.        a Standard File call may get a zeroed RefCon as a result.  This is 
  134.        true for Standard File 3.0 and later (System Software 5.0), as 
  135.        this is the first implementation of Standard File to use the List 
  136.        Manager.
  137.  
  138.  
  139. Putting It All Together
  140.  
  141. Here are most of the pieces put together.  InitTools and ShutDownStuff 
  142. routines have been omitted, but they are straightforward.
  143.  
  144. char            **y,*z;
  145. GrafPortPtr     myDialog;
  146. ListCtlRecHndl  myListHndl;
  147. CtlRecHndl      listScrollHandle;
  148. long            listRefCons, scrollRefCons;
  149.  
  150. #define Quit    ok
  151.  
  152. char            quitStr[] = "\pQuit";
  153.  
  154. ItemTemplate quitButton =  {
  155.         Quit,
  156.         140,450,154,590,
  157.         buttonItem,
  158.         quitStr,
  159.         0,
  160.         0,
  161.         NULL};
  162.  
  163. DialogTemplate PrintDialog = {
  164.         30,20,190,620,
  165.         false,
  166.         0,
  167.         &quitButton,
  168.         NULL};
  169.  
  170. char string1[] = "String1";
  171. char string2[] = "String2";
  172. char string3[] = "String3";
  173. char string4[] = "String4";
  174. char string5[] = "String5";
  175. char string6[] = "String6";
  176. char string7[] = "String7";
  177. char string8[] = "String8";
  178.  
  179.  
  180. MemRec  myMembers[8] = {
  181.         string1, 00,
  182.         string2, 00,
  183.         string3, 00,
  184.         string4, 00,
  185.         string5, 00,
  186.         string6, 00,
  187.         string7, 00,
  188.         string8, 00};
  189.  
  190. ListRec  myList = {
  191.         40,175,102,400, /* Enclosing Rectangle */
  192.         8,              /* Number of List Members */
  193.         6,              /* Max Viewable members */
  194.         3,              /* Bit Flag */
  195.         1,              /* First member in view */
  196.         NULL,           /* List control's handle */
  197.         NULL,           /* Address of Custom drawing routine */
  198.         10,             /* Height of list members */
  199.         5,              /* Size of Member Records */
  200.         (MemRecPtr)myMembers,/* Pointer to first element in MemRec[] */
  201.         NULL,           /* Becomes Control's refCon */
  202.         NULL            /* Color table for list's scroll bar */
  203.         };
  204.  
  205. /* ************************** */
  206.  
  207. main()
  208. {
  209.     word what;
  210.  
  211.     InitTools();        /* initialize tools */
  212.     InitValues();        /* Get dialog box. Install List control */
  213.     do {
  214.         what = ModalDialog((WordProcPtr)((long)myFilterProc | 0x80000000));
  215.     } while (what != Quit);
  216.     ShutDownStuff();  
  217. }
  218.  
  219. pascal Word myFilterProc(theDialog, theEvent, theItem)
  220.     GrafPortPtr    theDialog;
  221.     EventRecord    *theEvent;
  222.     long        *theItem;
  223.  
  224. {
  225.     CtlRecHndl    tHandle;
  226.  
  227.     if ((*theEvent).what == mouseDownEvt) {
  228.         FindControl(&tHandle,(*theEvent).where,theDialog);
  229.         if ((tHandle == myListHndl) || (tHandle == listScrollHandle)) {
  230.  
  231.             /* Set the RefCons back to the way the list manager likes them */
  232.             RestoreRefCons();
  233.             TrackControl((*theEvent).where,(LongProcPtr) -1, tHandle);
  234.             ZeroRefCons();
  235.  
  236.             /* Tell the Dialog Manager that we handled this event */
  237.             return(true);
  238.         }
  239.     }
  240.     /* We didn't do anything, so return false to get Dialog Manager
  241.         to handle this event */
  242.   return(false);
  243. }
  244.  
  245.  
  246. /* Zero out the Refcons for the Dialog Manager */
  247. ZeroRefCons()
  248. {
  249.     SetCtlRefCon(0,myListHndl);
  250.     SetCtlRefCon(0,listScrollHandle);
  251. }
  252.  
  253. /* Restore the Refcons for the List Manager */
  254. RestoreRefCons()
  255. {
  256.     SetCtlRefCon(listRefCons,myListHndl);
  257.     SetCtlRefCon(scrollRefCons,listScrollHandle);
  258. }
  259.  
  260. InitValues()
  261. {
  262. /* Get a Full Screen, invisible dialog window with only a Quit button in it*/
  263.     myDialog = GetNewModalDialog(&PrintDialog);
  264.  
  265.     /* Add this List Control ourselves */
  266.     myListHndl = CreateList(myDialog,&myList);
  267.  
  268.     /* Get the handle for the Scrollbar Control */
  269.     listScrollHandle = (**myListHndl).ctlListBar;
  270.  
  271.     /* Save and Zero out the RefCons */
  272.     listRefCons = GetCtlRefCon(myListHndl);
  273.     scrollRefCons = GetCtlRefCon(listScrollHandle);
  274.     ZeroRefCons();
  275.  
  276.     /* Now show the dialog box */
  277.     ShowWindow(myDialog);
  278. }
  279.  
  280.